package cn.lsnu.common.log.aop;



import cn.lsnu.common.log.entity.Log;
import cn.lsnu.common.log.service.LogService;
import cn.lsnu.utils.Guuid;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect//配置切面
public class LogAspect {

    @Autowired
    private HttpServletRequest request;

    @Resource(name="logService")
    public LogService logService;

    private Date startTime;

    private Class clazz;

    private Method method;

    private static Logger logger = Logger.getLogger(LogAspect.class);

    @Pointcut("execution(* cn.lsnu..controller.*.*(..))")
    private void logPointCut(){}

    @Before("logPointCut()")
    public void doBefore(JoinPoint jp) throws  Exception{
        //访问时间
        startTime = new Date();
        String targetName = jp.getTarget().getClass().getName();
        //获取访问类
        clazz = Class.forName(targetName); //jp.getTarget().getClass();
        //获取访问方法的名称
        String methodName = jp.getSignature().getName();
        //获取访问的方法参数
        Object[] args = jp.getArgs();
        Method[] methods = clazz.getMethods();
        for (Method mtd : methods) {
            if (mtd.getName().equals(methodName)) {
                Class[] clazzs = mtd.getParameterTypes();
                if(clazzs.length == args.length){
                    method = mtd;
                    break;
                }
            }
        }
//        if(args==null||args.length==0){
//            //获取无参方法
//            method = clazz.getMethod(methodName);
//        }else{
//            Class[] classeArgs = new Class[args.length];
//            for(int i=0;i<args.length;i++){
//                classeArgs[i] = args[i].getClass();
//           }
//            method = clazz.getDeclaredMethod(methodName,classeArgs);
//        }
    }

    @After("logPointCut()")
    public  void  doAfter()throws  Exception{
        long time = new Date().getTime() - startTime.getTime();
        String url="";
        String[] calssValue = null;
        String[] methodValue = null;
        if(clazz != null && method != null&&clazz!=LogAspect.class){
        //获取类上的@RequestMapping
        RequestMapping classAnnotation = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
        if(classAnnotation != null){
                calssValue = classAnnotation.value();
                url = calssValue[0];
            }
        //获取方法上的@RequestMapping
        RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
        if(methodAnnotation != null){
                methodValue = methodAnnotation.value();
            }
         url = url + methodValue[0];
        }

        String ip = request.getRemoteAddr();

        HttpSession session = request.getSession();

        String userName = (String) session.getAttribute("userName");

        Log log = new Log();
        log.setUserName(userName);
        log.setId(Guuid.uuid());
        log.setExecutionTime(time);
        log.setIp(ip);
        log.setUrl(url);
        log.setVisitTime(startTime);
        String methodStr = "";
        if(clazz != null){
            methodStr = "[类名] "+clazz.getName();
        }
        if(method != null){
            methodStr = methodStr+"[方法名] "+method.getName();
        }
        if(!clazz.getName().toString().contains("ValidateCodeControler")){
            log.setMethod(methodStr);
            logService.save(log);
            logger.info(log.toString());
        }
    }
}
